﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace LowLevelGraphics.Filter
{
    //s. miszalok / Kovalevsky
    /// <summary>
    /// Difference of Gaussian
    /// </summary>
    public class DoG :  BaseImageFilter
    {
        protected int m_n1 = 1;
        protected int m_n2 = 70;
        protected bool m_bInvert = false;

        protected ITwoSourceFilter twoSourceFilter = new Sub();
        protected GaussianBlur m_GaussianBlur = new GaussianBlur();
        /// <summary>
        /// Initializes a new instance of the <see cref="DoG"/> class.
        /// </summary>
        public DoG()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DoG"/> class.
        /// </summary>
        /// <param name="_n1">The _N1.</param>
        /// <param name="_n2">The _N2.</param>
        public DoG(int _n1, int _n2, bool _bInvert)
        {
            m_n1 = _n1;
            m_n2 = _n2;
            m_bInvert = _bInvert;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DoG"/> class.
        /// </summary>
        /// <param name="_dog">The _dog.</param>
        internal DoG(DoG _dog)
        {
            m_n1 = _dog.m_n1;
            m_n2 = _dog.m_n2;
            m_bInvert = _dog.m_bInvert;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap1 = (UnsafeBitmap)_bitmap.Clone();
            UnsafeBitmap bitmap2 = (UnsafeBitmap)_bitmap.Clone();
            
            m_GaussianBlur.Weight = m_n1;
            m_GaussianBlur.Execute(bitmap1);
            
            m_GaussianBlur.Weight = m_n2;
            m_GaussianBlur.Execute(bitmap2);
            UnsafeBitmap bitmap = twoSourceFilter.Process(bitmap1, bitmap2);

            BitmapFunctions.CopyUnsafe(bitmap, _bitmap);
            return _bitmap;
        }      

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new DoG(this);
        }
    }
}
